﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace MoneyWentWhere.WinFormsUI
{
    public partial class SetPasswordDialog : Form
    {
        public SetPasswordDialog()
        {
            InitializeComponent();
            UIStateUpdate();
        }

        void UIStateUpdate()
        {
            gPassword.Enabled = FileEncrypted;
            bOK.Enabled = !(FileEncrypted && String.IsNullOrEmpty(Password));

            if (FileEncrypted && 
                String.IsNullOrEmpty(tPassword.Text) &&
                String.IsNullOrEmpty(tConfirmPassword.Text))
            {
                lbError.Text = "Please enter and confirm password.";
            }
            else if (tPassword.Text != tConfirmPassword.Text)
            {
                lbError.Text = "Password and Confirm fields are different.";
            }
            else
            {
                lbError.Text = "";
            }
        }

        public String Password
        {
            get
            {
                if (tPassword.Text != tConfirmPassword.Text)
                {
                    return null;
                }
                return tPassword.Text;
            }
        }

        public bool FileEncrypted
        {
            get
            {
                return rbEncrypted.Checked;
            }
            set
            {
                rbEncrypted.Checked = value;
                UIStateUpdate();
                if (rbEncrypted.Checked)
                {
                    tPassword.Select();
                }
            }
        }

        private void rbEncrypted_CheckedChanged(object sender, EventArgs e)
        {
            UIStateUpdate();
        }

        private void tPassword_TextChanged(object sender, EventArgs e)
        {
            UIStateUpdate();
        }

        private void bOK_Click(object sender, EventArgs e)
        {
            DialogResult = DialogResult.OK;
            Close();
        }


    }
}
